package com.wkbb.sports.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.sports.dto.OrgGradeDto;
import com.wkbb.sports.dto.ScoreRuleDto;
import com.wkbb.sports.model.HlifeOrgGrade;
import com.wkbb.sports.service.RuleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@Api(description = "规则管理")
@RequestMapping("/sports/{apiVersion}/rule")
@Slf4j
public class RuleController extends BaseController {
	
	@Autowired
	private RuleService ruleService;

	@ApiOperation(value = "查询训练评分规则类型")
	@RequestMapping(value = "/queryScoreRule", method = RequestMethod.GET)
	public ResultDTO<ScoreRuleDto> queryScoreRule(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "项目类型", required = true) @RequestParam(name = "projectKind", required = true) Byte projectKind,
            @ApiParam(value = "年级ID", required = true) @RequestParam(name = "gradeId", required = true) Long gradeId)throws Exception{
		ScoreRuleDto rules = ruleService.queryScoreRule(gradeId,projectKind);
		return new ResultDTO<ScoreRuleDto>(rules);
	}

	@ApiOperation(value = "查询训练评分规则类型")
	@RequestMapping(value = "/queryScoreRuleTypes", method = RequestMethod.GET)
	public ResultDTO<?> queryScoreRuleTypes(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID类型", required = true) @RequestParam(name = "orgId", required = true) Long orgId)throws Exception{
		log.info("queryScoreRuleTypes -> orgId:{}", orgId);
		UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		List<ScoreRuleDto> rules = ruleService.queryScoreRuleTypeList(orgId);
		return new ResultDTO<List<ScoreRuleDto>>(rules);
	}

	@ApiOperation(value = "查询训练评分规则")
	@RequestMapping(value = "/queryScoreRules", method = RequestMethod.GET)
	public ResultDTO<?> queryScoreRules(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID") @RequestParam(name = "orgId", required=false) Long orgId)throws Exception{
//		log.info("queryScoreRules -> orgId:{}", orgId);
		UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		List<ScoreRuleDto> rules = ruleService.queryScoreRuleList(orgId);
		return new ResultDTO<List<ScoreRuleDto>>(rules);
	}
	
	@ApiOperation(value = "获取对应规则")
	@RequestMapping(value = "/queryOrgGradeMappingList", method = RequestMethod.GET)
	public ResultDTO<?> queryOrgGradeMappingList(
			@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "学校ID") @RequestParam(name = "orgId", required=false) Long orgId)throws Exception{
		List<HlifeOrgGrade> list = ruleService.queryOrgGradeList(orgId);
		List<OrgGradeDto> dtos = new ArrayList<>();
		for(HlifeOrgGrade orgGrade : list) {
			OrgGradeDto dto = new OrgGradeDto();
			dto.setGradeId(orgGrade.getGradeId());
			dto.setRuleGradeId(orgGrade.getRuleGradeId());
			dtos.add(dto);
		}
		return new ResultDTO<>(dtos);
	}
}
